<%!String title = "Context info";%>

<%@ include file="/header.jsp"%>

<script type="text/javascript">
var extraToppings = 0;
var price = <% out.print(sb.getTotalPrice()); %>;

function addToppingToPizza(selector){
	//alert(price);
    var myindex  = selector.selectedIndex;
    var selValue = selector.options[myindex].value;
	var pizzaId = selector.id;  //id of the pizza to add topping to
	
	if(selValue!="Pick extra topping..."){
		selector.disabled=true; //removing (undoing) an extra topping is not an option..
		
		var xpath_quantity = document.getElementsByTagName('td');
		//alert(xpath_quantity.length);
		for (var i = 0; i < xpath_quantity.length; i++){
			var tempQuantity = xpath_quantity[i];
			//alert(tempQuantity);
			if (tempQuantity.title == pizzaId){
				var text = tempQuantity.childNodes[0].data.substr(10);
				var quantity = parseInt(text);
				extraToppings += quantity;
				
				//alert("ExtraToppings: " + extraToppings);
			}
		}	
		
		

		//access table with pizza info
		var t = document.getElementsByTagName("table")[0];
		var tr = t.getElementsByTagName("tr");
		var lastTr = tr[tr.length-1];
		var td = lastTr.getElementsByTagName("td")[1];
		var tmpF = td.childNodes[0].data;
		//alert(tmpF);
		tmpF = (price +(5*extraToppings)).toFixed(1);
		//alert("tmpF: " + tmpF);
		td.childNodes[0].data = tmpF;
		
		var xpath_add = document.evaluate("//input[@name='basket_add']" ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
		var xpath_remove = document.evaluate("//input[@name='basket_remove']" ,document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

		for (var i = 0; i < xpath_add.snapshotLength; i++){
			var tempButton = xpath_add.snapshotItem(i);
			tempButton.disabled=true;
		}	
		for (var i = 0; i < xpath_remove.snapshotLength; i++){
			var tempButton = xpath_remove.snapshotItem(i);
			tempButton.disabled=true;	
		}
	}	
	//alert("pizzaId " + pizzaId);
	//console.log("pizzaid " + sid);
}

function newTopping(pizzaid)  
{ 
	var allToppings = ["Pick extra topping...", "Bacon", "Cheese", "Chili", "Onion", "Ham", "Pepperoni", "Beef", "Pineapple", "Tomatoes", "Pepper", "Garlic", "Mushroom", "Tuna", "Egg"];
	
	var input = document.createElement("form");
	var select =  document.createElement("select");
	select.id = pizzaid;
	select.setAttribute("name", "selector");
	select.setAttribute("onChange", "addToppingToPizza(selector)");
	for(i=0; i<allToppings.length; i++) { 
		var option = document.createElement("option");
		var text = document.createTextNode(allToppings[i]);
		option.appendChild(text);
		select.appendChild(option);
	}
	input.appendChild(select);
	document.getElementById(pizzaid).appendChild(input);

	
}  

function goToCheckout() {
	//alert("goToCheckout method reached");
	window.location="checkout.jsp?toppings=" + extraToppings;
}


</script> 

<h1>Basket</h1>
<br>
<table>
	<%
		if (sb != null) {
			ArrayList<Pizza> basketPizzas = sb.getPizzas();
			if (basketPizzas != null) {
				for (int i = 0; i < basketPizzas.size(); i++) {
					Pizza p = basketPizzas.get(i);
					int id = p.getId();
					HashMap<Integer, Integer> map = sb.getPizzaMap();
					int quantity;
					if(map.containsKey(id))
						quantity = (Integer) map.get(id);
					else quantity = 1;
					
					out.print("<tr><td>"
							+ p.toString()
							+ "</td><td title=" + id + " id=\"quantity\"> Quantity:"
							+ quantity
							+ "</td> <td> <form method=\"post\" action=\"controller\">"
							+ "<input type=\"submit\" name=\"basket_remove\" value=\"-\">"
							+ "<input type=\"hidden\" name=\"basket_pizzaid\" value="
							+ id + "></form></td>");
					out.print("<td>"
							+ "<form method=\"post\" action=\"controller\">"
							+ "<input type=\"submit\" name=\"basket_add\" value=\"+\">"
							+ "<input type=\"hidden\" name=\"basket_pizzaid\" value="
							+ id + "></form></td></tr>");
					out.print("<tr><td> <form id=" + id + ">"
							+ "<input type=\"button\" name=\"basket_topping\" value=\"Add topping\" onClick=newTopping(" + id + ")>"
							+ " at additional cost of 5.00kr </form></td>");

				}
				out.print("<tr><td>TOTAL PRICE: </td>"
						+ "<td>" + sb.getTotalPrice() + "</td></tr>");
			}
		}
	%>
</table>
<!-- a href="checkout.jsp">Proceed to checkout</a-->
<input type="submit" value="Proceed to checkout" onClick="goToCheckout()" />
</body>
</html>
